<?php $sessionBuy = $params['sessionBuy'];?>
<style type="text/css" xmlns="http://www.w3.org/1999/html">
.box-block {
	padding: 29px 29px 19px 29px;
	margin: 0 auto 20px;
	background-color: #FFF;
	border: 1px solid #E5E5E5;
	-webkit-border-radius: 5px;
	-moz-border-radius: 5px;
	border-radius: 5px;
	-webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, .14);
	-moz-box-shadow: 0 1px 2px rgba(0, 0, 0, .14);
	box-shadow: 0 1px 2px rgba(0, 0, 0, .14);
}

.confirmBuyPackageBlock .form-horizontal .read {
    line-height: 28px;
}

.confirmBuyPackageBlock .form-horizontal label{
	text-align: left;
	width: 160px;
}
</style>
<div class="box-block confirmBuyPackageBlock">
	<h3>Invoice</h3>
	<table class="table">
		<thead>
			<tr class="success">
				<th>รายการ</th>
				<th>รายละเอียด</th>
				<th>ระยะเวลา</th>
				<th>ค่าใช้จ่าย</th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td><?php echo $sessionBuy['package']['name'];?></td>
				<td><?php echo nl2br($sessionBuy['info']);?></td>
				<td><?php echo $sessionBuy['price']['month'];?> เดือน</td>
				<td><?php echo Helper::formatMoney($sessionBuy['price']['price']);?> บาท</td>
			</tr>
			<tr class="success">
				<td></td>
				<td></td>
				<td><strong>รวม</strong></td>
				<td><?php echo Helper::formatMoney($sessionBuy['price']['price']);?> บาท</td>
			</tr>
		</tbody>
	</table>
	<h4>รายละเอียดผู้สั่งซื้อ(สำหรับออกใบเสร็จ)</h4>
	<form method="post" action="index.php?page=confirm.buyPackage" class="form-horizontal confirm-form">
        <div class="control-group">
            <label class="control-label">ใช้ข้อมูลของ</label>
            <div class="controls">
                <label class="radio">
                    <input type="radio" name="profile" value="me" checked /> ฉัน
                </label>
                <label class="radio">
                    <input type="radio" name="profile" value="other" /> บุคคลอื่น
                </label>
            </div>
        </div>
        <div class="me-section">
            <?php
            $account = Account::instance()->getOne(Authenticate::getId());
            $name = trim($account['name']);
            $address = trim($account['address']);
            $phone = trim($account['phone']);
            ?>
            <div class="control-group">
                <label class="control-label">ชื่อ-นามสกุล/บริษัท</label>
                <div class="controls">
                    <?php if(empty($name)){?>
                    <input type="text" name="me[name]" />
                    <span class="help-inline"></span>
                    <?php }else{?>
                    <span class="read"><?php echo $name;?></span>
                    <?php }?>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label">ที่อยู่</label>
                <div class="controls">
                    <?php if(empty($address)){?>
                    <textarea name="me[address]"></textarea>
                    <span class="help-inline"></span>
                    <?php }else{?>
                    <span class="read"><?php echo $address;?></span>
                    <?php }?>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label">โทรศัพท์</label>
                <div class="controls">
                    <?php if(empty($phone)){?>
                    <input type="text" name="me[phone]" />
                    <span class="help-inline"></span>
                    <?php }else{?>
                    <span class="read"><?php echo $phone;?></span>
                    <?php }?>
                </div>
            </div>
        </div>
        <div class="other-section" style="display: none;">
            <div class="control-group">
                <label class="control-label">ชื่อ-นามสกุล/บริษัท</label>
                <div class="controls">
                    <input type="text" name="other[name]" />
                    <span class="help-inline"></span>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label">ที่อยู่</label>
                <div class="controls">
                    <textarea name="other[address]"></textarea>
                    <span class="help-inline"></span>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label">โทรศัพท์</label>
                <div class="controls">
                    <input type="text" name="other[phone]" />
                    <span class="help-inline"></span>
                </div>
            </div>
        </div>
		<input type="hidden" name="confirmCode" value="<?php echo $params['sessionCode'];?>" />
	</form>
	<div class="text-right">
		<a href="javascript:window.location.reload();"><button class="btn">Cancel</button></a>
		<button class="btn confirm-button">Confirm</button>
	</div>
</div>
<script type="text/javascript">
$(function(){
    var form = $('.confirmBuyPackageBlock .confirm-form');

	$('.confirmBuyPackageBlock .confirm-button').click(function(){
        form.submit();
	});

    $('[name="profile"]', form).change(function(event){
        var val = $(this).val();
        if(val=='me'){
            $('.other-section').slideUp();
            $('.me-section').slideDown();
        }
        else {
            $('.me-section').slideUp();
            $('.other-section').slideDown();
        }
    });

    form.submit(function(event){
        var ConfirmException = function(message, code, fields){
            this.message = message;
            this.code = code;
            this.fields = typeof fields=='undefined'? []: fields;
        };
        ConfirmException.prototype.fields = [];

        try {
            var type = $('[name="profile"]:checked', form).val();
            if(type=='me'){
                var eles = $('.me-section', form).find('input, textarea');
                eles.closest('.control-group').removeClass('error');
                var fields = [];
                if(eles.length>0){
                    eles.each(function(index, el){
                        el = $(el);
                        if($.trim(el.val())==''){
                            //el.data('helper-text', 'กรุณากรอกข้อมูล');
                            fields.push(el);
                        }
                    });
                    if(fields.length>0){
                        throw new ConfirmException('',0,fields);
                    }
                }
            }
            else {
                var eles = $('.other-section', form).find('input, textarea');
                eles.closest('.control-group').removeClass('error');
                var fields = [];
                if(eles.length>0){
                    eles.each(function(index, el){
                        el = $(el);
                        if($.trim(el.val())==''){
                            //el.data('helper-text', 'กรุณากรอกข้อมูล');
                            fields.push(el);
                        }
                    });
                    if(fields.length>0){
                        throw new ConfirmException('',0,fields);
                    }
                }
            }
        } catch(e) {
            event.preventDefault();
            $.each(e.fields, function(index, el){
                el = $(el);
                var controlGrop = el.closest('.control-group');
                controlGrop.addClass('error');
                //$('.help-inline', controlGrop).text(el.data('helper-text'));
            });
        }
    });
});
</script>